package utils
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import events.MyEvent;
	/**
	 * ...
	 * @author FireKnight
	 */
	public class MakeCircleEvent extends EventDispatcher
	{
		//顺时针和逆时针圆绘画完毕消息
		static public const EVENT_CWCOMPLETE:String = "EVENT_CWCOMPLETE";
		static public const EVENT_CCWCOMPLETE:String = "EVENT_CCWCOMPLETE";
		static public const EVENT_ROTATION:String = "EVENT_ROTATION";
		private var _isDown:Boolean = false;//判断鼠标是否按下
		private var _isDispatched:Boolean = false;//用于按下一次只抛出一次事件
		//圆中心位置
		private var _circleCenter:Point = new Point();

		private var _cw:Boolean;//顺时针标记

		private var _cwComplete:Boolean;//顺时针完成标记

		private var _ccw:Boolean;//逆时针标记
		private var _ccwComplete:Boolean;//逆时针过半标记
		private var _parent:DisplayObject;
		private var _lastMouseX:Number;
		private var _lastMouseY:Number;
		private var startDegree:Number;
		
		private var fx:Vector.<Number> = new Vector.<Number>();//记载方向
		public function MakeCircleEvent(__parent:DisplayObject,circleXOff:Number = 190,circleYOff:Number =190,target:IEventDispatcher=null)
		{
			super(target);
			_parent = __parent;
			_circleCenter.x = circleXOff;
			_circleCenter.y = circleYOff;
			_cw = _ccw = false;
			
		}

		public function onMouseDown(event:MouseEvent):void
		{
			//鼠标左键被按下
			fx = null;
			fx = new Vector.<Number>();
			_isDown = true;
			startDegree = keepRot(getDegree());
			_lastMouseX = _parent.mouseX;
			_lastMouseY = _parent.mouseY;
			_isDispatched = false;
		}

		public function onMouseUp(event:MouseEvent):void
		{
			//鼠标左键松开
			_isDown = false;
			//重置状态
			_cw = _ccw = false;
			_cwComplete = _ccwComplete = false;
			_isDispatched = false;
			//trace("松开="+keepRot(getDegree()));
		}

		public function onEnter(event:Event):void
		{
			if (_isDown)
			{
				var degree:Number = getDegree();
				getFX(keepRot(degree));
				getClock(keepRot(degree));
				this.dispatchEvent(new MyEvent(EVENT_ROTATION,degree ));
				completeAction();	
				if (_cwComplete &&_isDispatched==false)
				{
					_cwComplete = false;
					_cw = false;
					_ccwComplete = false;
					_ccw = false;
					_isDispatched = true;
					fx = null;
					fx = new Vector.<Number>();
					//trace("完成顺时针画圆");
					this.dispatchEvent(new Event(EVENT_CWCOMPLETE));

				}

				if (_ccwComplete && _isDispatched==false)
				{
					_cwComplete = false;
					_cw = false;
					_ccwComplete = false;
					_ccw = false;
					fx = null;
					fx = new Vector.<Number>();
					_isDispatched = true;
					//trace("完成逆时针画圆");
					this.dispatchEvent(new Event(EVENT_CCWCOMPLETE));
				}
			}
		}
		
		
		
		private function completeAction():void
		{
			if (fx)
			{
				
				if (fx.length >2)
				{
					if (fx.length > 3)
					{
						fx.shift();
					}
						if (fx[1] == 0)
						{
							if (fx[0] == 1)
							{
								if (fx[2] == 3)
								{
									_ccwComplete = true;
									_cwComplete = false;
									//trace("_ccwComplete");
								}
								
							}
							else if (fx[0] == 3)
							{
								if (fx[2] == 1)
								{
									_cwComplete = true;
									_ccwComplete = false;
									//trace("_cwComplete");
								}
							}
						}
						else if (fx[1] == 1)
						{
							if (fx[0] == 2)
							{
								if (fx[2] == 0)
								{
									_ccwComplete = true;
									_cwComplete = false;
									//trace("_ccwComplete");
								}
								
							}
							else if (fx[0] == 0)
							{
								if (fx[2] == 2)
								{
									_cwComplete = true;
									_ccwComplete = false;
									//trace("_cwComplete");
								}
							}
						}
						else if (fx[1] == 2)
						{
							if (fx[0] == 1)
							{
								if (fx[2] == 3)
								{
									_cwComplete = true;
									_ccwComplete = false;
									//trace("_cwComplete");
								}
							}
							else if (fx[0] == 3)
							{
								if (fx[2] == 1)
								{
									_ccwComplete = true;
									_cwComplete = false;
									//trace("_ccwComplete");
								}
							}
						}
						else if (fx[1] == 3)
						{
							if (fx[0] == 0)
							{
								if (fx[2] == 2)
								{
									_ccwComplete = true;
									_cwComplete = false;
									//trace("_ccwComplete");
								}
							}
							else if (fx[0] == 2)
							{
								if (fx[2] == 0)
								{
									_cwComplete = true;
									_ccwComplete = false;
									//trace("_cwComplete");
								}
							}
						}
				}
			}
		}
		
		private function getClock(degree:Number):void
		{
			
			var _starDegree:Number = keepRot(startDegree);
			if (_starDegree >= 0 && _starDegree < 90)
			{
				if (degree >= 270 && degree <= 359)
				{
					_ccw = true;
					_cw = false;
				}
				else
				{
					if (degree-_starDegree >= 0)
					{
						_ccw = false;
						_cw = true;
					}
					else
					{
						_ccw = true;
						_cw = false;
					}
				}
			}
			else if(_starDegree >= 270 && _starDegree < 359)
			{
				if (degree >= 0 && degree <= 90)
				{
					_ccw = false;
					_cw = true;
				}
				else
				{
					if (degree-_starDegree >= 0)
					{
						_ccw = false;
						_cw = true;
					}
					else
					{
						_ccw = true;
						_cw = false;
					}
				}
			}
		}

		private function getFX(degree:Number):void
		{
			 //把角度分为四个分区
			if (degree >= 0 && degree < 90)
			{
				
				fxAddItem(0);
			}
			else if (degree >= 90 && degree < 180)
			{
				fxAddItem(1);
			}
			else if (degree >= 180 && degree < 270)
			{
				fxAddItem(2);
			}
			else if (degree >= 270 && degree <= 359)
			{
				fxAddItem(3);
			}
		}
		
		private function fxAddItem(s:Number):void
		{
			if (!fx) return;
			if (fx.length > 0)
			{
				if (fx[fx.length - 1] == s) return;
			}
			
			if (fx.length == 0 || fx.length <3)
			{
				fx.push(s);
			}
			else if (fx.length >= 3)
			{
				fx.shift();
				fx.push(s);
			}
			
		}

		private function getDegree():Number
		{
			var dx:Number = _parent.mouseX - _circleCenter.x;
			var dy:Number = _parent.mouseY - _circleCenter.y;
			var radians:Number = Math.atan2(dy,dx);
			var degress:Number = radians / Math.PI * 180;
			degress = Math.floor(degress);
			return degress;
		}

		private function getRadians():Number
		{
			var dx:Number = _parent.mouseX - _circleCenter.x;
			var dy:Number = _parent.mouseY - _circleCenter.y;
			var radians:Number = Math.atan2(dy,dx) * 100;

			return radians;
		}

		private function keepRot(s:Number):Number
		{
			if (s<0)
			{
				s = 360 + s;
			}
			else if (s==360)
			{
				s = 0;
			}
			else if (s>360)
			{
				s = s - 360;
			}
			return s;
		}

	}

}